import type { ErrorMessageMode } from "/#/axios";
import { useI18n } from "@/hooks/web/useI18n";
// import router from '/@/router';
// import { PageEnum } from '/@/enums/pageEnum';
import store from "@/store";
import { SessionTimeoutProcessingEnum } from "@/enums/appEnum";
import projectSetting from "@/settings/projectSetting";
import { ElNotification } from "element-plus";
import { removeToken } from "@/utils/auth";
const stp = projectSetting.sessionTimeoutProcessing;

export function checkStatus(
  status: number,
  msg: string,
  errorMessageMode: ErrorMessageMode = "message"
): void {
  const { t } = useI18n();
  let errMessage = "";

  switch (status) {
    case 400:
      errMessage = `${msg}`;
      break;
    // 401: Not logged in
    // Jump to the login page if not logged in, and carry the path of the current page
    // Return to the current page after successful login. This step needs to be operated on the login page.
    case 401:
      // store.dispatch("setToken", undefined);
      errMessage = msg || t("sys.api.errMsg401");
      if (stp === SessionTimeoutProcessingEnum.PAGE_COVERAGE) {
        store.dispatch("setSessionTimeout", true);
      } else {
        // 如果有请求没有token报错不需要清空token,到期会自动清除
        removeToken();
      }
      break;
    case 403:
      errMessage = t("sys.api.errMsg403");
      break;
    // 404请求不存在
    case 404:
      errMessage = t("sys.api.errMsg404");
      break;
    case 405:
      errMessage = t("sys.api.errMsg405");
      break;
    case 408:
      errMessage = t("sys.api.errMsg408");
      break;
    case 500:
      errMessage = t("sys.api.errMsg500");
      break;
    case 501:
      errMessage = t("sys.api.errMsg501");
      break;
    case 502:
      errMessage = t("sys.api.errMsg502");
      break;
    case 503:
      errMessage = t("sys.api.errMsg503");
      break;
    case 504:
      errMessage = t("sys.api.errMsg504");
      break;
    case 505:
      errMessage = t("sys.api.errMsg505");
      break;
    default:
      errMessage = msg;
      break;
  }
  console.log(status);
  console.log(errMessage);

  if (errMessage) {
    if (errorMessageMode === "modal") {
      console.log(`t('sys.api.errorTip')`);
      ElNotification.error({
        message: `t('sys.api.errorTip')`,
        duration: 5000,
      });
      // createErrorModal({ title: t('sys.api.errorTip'), content: errMessage });
    } else if (errorMessageMode === "message") {
      // error({ content: errMessage, key: `global_error_message_status_${status}` });
      console.log(`${errMessage}`);
      ElNotification.error({
        message: `${errMessage}`,
        duration: 5000,
      });
    }
  }
}
